package com.yanxu.chapter1;

import com.yanxu.domain.Metaspace;
import com.yanxu.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2024/11/17-2024
 * @Description: 演示内存溢出的情况
 */
@RestController
@RequestMapping("/memory")
@Slf4j
public class MemoryController {

    private List<User> userList = new ArrayList<>();
    private List<Class<?>> classList = new ArrayList<>();

    /**
     * 堆内存溢出情况演示：
     *      -Xmx:32M  -Xms:32M
     *       等价于
     *      -XX:MaxHeapSize=32M   -XX:InitialHeapSize=32M
     */
    @GetMapping("/heapMemory")
    public void heapMemory(){
        log.info("堆内存溢出情况演示!!!");
        int i=0;
        while (true){
            String name = UUID.randomUUID().toString();
            userList.add(new User(name,i++));
        }
    }

    /**
     * 非堆内存溢出情况演示：
     *      -XX:MetaspaceSize=32M  -XX:MaxMetaspaceSize=32M
     */
    @GetMapping("/nonHeap")
    public void nonHeap(){
        log.info("Metaspace 空间溢出演示!!!");
        while (true){
            classList.addAll(Metaspace.createClasses());
        }
    }
}